<?php
    // Error Reporting

    error_reporting(E_ALL);
    ini_set("display_errors", "on");

    header('Content-type: image/gif');
    
    include('lzw.php');

    $addToFlatfile = true;
    if (isset($_GET["e"])) {
        $userPath = "data/flatfiles/".$_GET["u"];
        if (!is_dir($userPath)) {
            mkdir($userPath);
        }
        $filename = $_GET["r"].".txt";
        foreach($_GET as $key => $value) {
            if ($key != "e") {
                $params = urldecode($key);
            }
        }
        $addToFlatfile = false;
        $flatHandler = fopen($userPath."/".$filename, "a");
        fwrite($flatHandler, $params);
        fclose($flatHandler);
        if ($_GET["e"] == "0") {
            $addToFlatfile = true;
            $flatHandler = fopen($userPath."/".$filename, "r");
            $params = decompress(base64_decode(fgets($flatHandler)));
            fclose($flatHandler);
            unlink($userPath."/".$filename);
        }
        touch($userPath);
    }
    if ($addToFlatfile) {
        if (!isset($_GET["e"])) {
            foreach($_GET as $key => $value) {
                $params = decompress(base64_decode(urldecode($key)));
                break;
            }
        }
        $params = explode("&", $params);
        $finalParams = array();
        foreach($params as $param) {
            $paramArr = explode("=", $param);
            $finalParams[$paramArr[0]] = $paramArr[1];
        }
        $timestamp = htmlentities($finalParams['timestamp']);
        $user_id = htmlentities($finalParams['user_id']);
        $data = json_decode(stripslashes($finalParams['json']));
        
        $userPath = "data/flatfiles/".$user_id;
        if (is_dir($userPath)) {
            $filename = $timestamp.".txt";
            $entry = "";
            $savedUntil = 0;
            $allDataSaved = false;
            if (!file_exists($userPath."/".$filename)) {
                $entry .= "start::".time()."\n";
            } else {
                $oldEntries = file($userPath."/".$filename);
                for ($i = 1; (count($oldEntries) - $i) >= 0; $i++) {
                    $oldEntries = $oldEntries[count($oldEntries) - $i];
                    $oldEntries = explode("::", $oldEntries);
                    if ($oldEntries[0] == "p" || $oldEntries[0] == "w" || $oldEntries[0] == "b") {
                        $oldEntries = explode("|", $oldEntries[1]);
                        $savedUntil = $oldEntries[2];
                        break;
                    } else if ($oldEntries[0] == "sessiontime") {
                        $allDataSaved = true;
                        break;
                    }
                }
            }
            if (!$allDataSaved) {
                $startSaving = false;
                $waitLines = 0;
                if ($savedUntil == 0) {
                    $startSaving = true;
                    $skippedLines = 0;
                    $waitLines = 1;
                    $flatHandler = fopen($userPath."/".$filename, "w");
                } else {
                    $skippedLines = $i - 2;
                    $flatHandler = fopen($userPath."/".$filename, "a");
                }
                foreach($data as $key => $value) {
                    if (is_array($value)) {
                        foreach ($value as $arrValue) {
                            if ($startSaving) {
                                if ($waitLines > $skippedLines) {
                                    $entry .= $arrValue."\n";
                                } else {
                                    $waitLines++;
                                }
                            } else {
                                $arrEntry = explode("::", $arrValue);
                                if ($arrEntry[0] == "p" || $arrEntry[0] == "w" || $arrEntry[0] == "b") {
                                    $arrEntry = explode("|", $arrEntry[1]);
                                    if ($arrEntry[2] >= $savedUntil) {
                                        $startSaving = true;
                                    }
                                }
                            }
                        }
                    } else if ($startSaving) {
                        if ($waitLines > $skippedLines) {
                            $entry .= $key."::".$value."\n";
                        } else {
                            $waitLines++;
                        }
                    }
                }
                fwrite($flatHandler, $entry);
                fclose($flatHandler);
            }
        }
    }
    // include('img/tm_pixel.gif');
?>
